/*
    7. Write a program in C to count a number of words and characters in a file. 
    Test Data :
    Input the file name to be opened : test.txt
    Expected Output :

    The content of the file test.txt are :                                                                       
    test line 1                                                                                                   
    test line 2                                                                                                   
    test line 3                                                                                                   
    test line 4                                                                                                   
    The number of words in the  file test.txt are : 12                                                           
    The number of characters in the  file test.txt are : 36
*/

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    FILE *fp;
    char filename[64];
    char ch;
    int lines = 0;
    int inflag = 0, outflag = 0;
    int wcnt = 0, chcnt = 0;
    printf("Input the file name to be opened : ");
    scanf("%s", filename);
    fp = fopen(filename, "r");
    while((ch = fgetc(fp)) != EOF){
        putc(ch, stdout);
        if(isalnum(ch) && inflag == 0){
            inflag = 1;
        }
        if((isblank(ch) || ch == '\n') && inflag == 1){
            inflag = 0;
            outflag = 1;
        }
        if(outflag){
            outflag = 0;
            wcnt++;
        }
        if(isalnum(ch)){
            chcnt++;
        }
    }
    printf("\nThe number of words in the file %s are : %d\n", filename, wcnt);
    printf("The number of character in the file %s are : %d\n", filename, chcnt);

    return 0;
}